package com.it.config;

import com.it.bean.Color;
import com.it.bean.ColorFacotryBean;
import com.it.bean.Person;
import com.it.bean.Red;
import com.it.condition.LinuxCondition;
import com.it.condition.MyImportBeanDefinitionRegistrar;
import com.it.condition.MyImportSelector;
import com.it.condition.WindowCondition;
import org.springframework.context.annotation.*;
/**
 * 给容器中注册bean
 * 1 包扫描+组件标注注解（@Controller/@Service/@Respository/@Component）
 * 2 @Bean[导入的第三方里面的组件]
 * 3 @Import[快速给容器中导入一个组件]
 *      1 @Import(要导入的组件) 容器中就会自动注册这个组件，id默认是全类名
 *      2 ImportSelector 返回需要导入的组件的全类名数组
 *      3 ImportBeanDefinitionRegistrar 手动注册bean到容器中
 * 4 使用Spring提供的FactoryBean(工厂Bean)
 *      1 默认获取到的是工厂bean调用getObject创建的对象
 *      2 要获取工厂Bean本身，需要给id前加一个& e.g. &colorFactoryBean
 */
@Configuration
@Import({Color.class, Red.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
public class MyConfig2 {

    // @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Bean("person")
    @Lazy
    public Person person() {
        System.out.println("容器中创建Person");
        return new Person("张三", 25);
    }

    @Conditional({WindowCondition.class})
    @Bean("比尔")
    public Person person1() {
        return new Person("比尔", 100);
    }

    @Conditional({LinuxCondition.class})
    @Bean("维纳斯")
    public Person person2() {
        return new Person("维纳斯", 60);
    }

    @Bean
    public ColorFacotryBean colorFacotryBean(){
        return new ColorFacotryBean();
    }



}
